package com.kws.payment.controller;

import cn.hutool.json.JSONUtil;
import com.kws.common.core.domain.R;
import com.kws.payment.dao.CreateAppUnionPayOrderRq;
import com.kws.payment.dao.UnionPayRefundRq;
import com.kws.payment.dao.info.CreateAppUnionPayOrderInfo;
import com.kws.payment.dao.info.PaymentCallbackInfo;
import com.kws.payment.service.UnionAPPPayService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <b>UnionAPPPayController</b>
 *
 * @description: UnionAPPPayController <br>
 * @date: 2024/12/25 09:24 <br>
 * @author: lhm <br>
 * @version: 1.0 <br>
 */

@Api(tags = "银联APP支付")
@RestController
public class UnionAPPPayController {
    private static final Logger logger = LoggerFactory.getLogger(UnionAPPPayController.class);

    @Autowired
    private UnionAPPPayService unionAPPPayService;

    @ApiOperation("创建微信下单")
    @PostMapping("/create/app/weixinUnionPayOrder")
    public R<CreateAppUnionPayOrderInfo> createAppWeixinUnionPayOrder(@RequestBody CreateAppUnionPayOrderRq rq) throws Exception {
        logger.info("创建银联支付app微信订单入参:{}"+ JSONUtil.parse(rq));
        CreateAppUnionPayOrderInfo info = unionAPPPayService.createAppWeixinUnionPayOrder(rq);
        return R.ok(info);
    }


    @ApiOperation("创建支付宝下单")
    @PostMapping("/create/app/aliUnionPayOrder")
    public R<CreateAppUnionPayOrderInfo> createAppAliUnionPayOrder(@RequestBody CreateAppUnionPayOrderRq rq) throws Exception {
        logger.info("创建银联支付app端支付宝订单入参:{}"+JSONUtil.parse(rq));
        CreateAppUnionPayOrderInfo info = unionAPPPayService.createAppAliUnionPayOrder(rq);
        return R.ok(info);
    }

    @ApiOperation("创建云闪付下单")
    @PostMapping("/create/app/yunFlashPayOrder")
    public R<CreateAppUnionPayOrderInfo> createAppYunFlashPayOrder(@RequestBody CreateAppUnionPayOrderRq rq) throws Exception {
        logger.info("创建云闪付西单入参:{}"+JSONUtil.parse(rq));
        CreateAppUnionPayOrderInfo info = unionAPPPayService.createAppYunFlashPayOrder(rq);
        return R.ok(info);
    }

    @ApiOperation("根据订单ID取消订单")
    @GetMapping("/close/unionPayOrder")
    public R closeUnionPayOrder(@RequestParam String orderNo) throws Exception {
        CreateAppUnionPayOrderInfo info = unionAPPPayService.closeUnionPayOrder(orderNo);
        return R.ok(info);
    }

    @ApiOperation("银联支付退款")
    @PostMapping("/unionPay/app/refund")
    public R unionPayAppRefund(@RequestBody UnionPayRefundRq rq) throws Exception {
        logger.info("发起银联支付退款入参:{}"+JSONUtil.parse(rq));
        CreateAppUnionPayOrderInfo info = unionAPPPayService.unionPayAppRefund(rq);
        return R.ok(info);
    }

    @ApiOperation("支付结果通知-app支付结果回调并且可做退款回调")
    @PostMapping("/unionPay/paymentResult")
    public void unionPayPaymentResult(@RequestBody PaymentCallbackInfo resultInfo){
        logger.info("获取银联支付结果通知信息:{}"+JSONUtil.parse(resultInfo));
        unionAPPPayService.unionPayPaymentResult(resultInfo);
    }

}
